<?php
/**
 * created by zzy
 * date: 2018/1/16 10:48
 */
namespace App\Eloquent\Ygt;

use Illuminate\Database\Eloquent\SoftDeletes;

class BillAccount extends DbEloquent
{
    use SoftDeletes;
    protected $dates    = ['deleted_at'];
    protected $table    = 'ygt_bill_account';
    public static $tableFieldList   = [
        ['name'=>'id','title'=>'','value'=>0,'is_must'=>0],
        ['name'=>'company_id','title'=>'登录用户异常','value'=>0,'is_must'=>1],
        ['name'=>'pid','title'=>'上级分类','value'=>0,'is_must'=>0,'type'=>1],
        ['name'=>'title','title'=>'名称','value'=>'','is_must'=>1],
    ];

    //取厂自定义的账户
    public static function getChangeList($where)
    {
        $fields             = 'id,company_id,pid,title';
        $lists              = self::getList($where,$fields)->toArray();
        $result             = self::changeList($lists);
        return $result;
    }
    //$type 1时一级分类包含二级分类,2时只返回列表不区分一级分类和二级分类
    public static function changeList($lists)
    {
        $parentList     = $listArr = [];
        foreach($lists as $key=>$val)
        {
            $valId          = $val['id'];
            $valPid         = $val['pid'];
            $valTitle       = $val['title'];
            $temp           = [
                'id'=>$valId, 'pid'=>$valPid,'title'=>$valTitle
            ];
            if($valPid == 0){
                $temp['data']       = [];
                $parentList[]       = $temp;
            }else{
                $listArr[$valPid][] = $temp;
            }
        }
        $result         = [];
        foreach($parentList as $key=>$val)
        {
            $valId          = $val['id'];
            $temp           = $val;
            if(isset($listArr[$valId]))
            {
                $temp['data']   = $listArr[$valId];
            }
            $result[]       = $temp;
        }
        return $result;
    }
}
